package frame.Customer;

import MyBatis.MyBatislianjian;
import frame.GuanliPanel;
import org.apache.ibatis.session.SqlSession;
import org.example.mapper.CustomerMapper;
import org.example.pojo.Customer;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

public class customerselect extends JInternalFrame {
    private JTextField[] inputFields = new JTextField[8]; // 输入框数组
    private JLabel[] inputLabels = new JLabel[8]; // 输入框说明标签数组
    private JTable resultTable; // 显示查询结果的表格
    private DefaultTableModel tableModel;
    private SqlSession sqlSession = MyBatislianjian.getSqlSession();
    private CustomerMapper customerMapper = sqlSession.getMapper(CustomerMapper.class);

    public customerselect() {
        setTitle("查询");
        setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE);
        setLayout(new BorderLayout());

        // 创建顶部的查询输入区域
        JPanel inputPanel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 10, 5, 10); // 设置组件间距

        String[] labels = {"客户编号", "公司名称", "公司简称", "公司地址", "公司电话", "公司邮件", "联系人姓名", "联系人电话"};

        for (int i = 0; i < labels.length; i++) {
            inputLabels[i] = new JLabel(labels[i] + ": ");
            inputLabels[i].setFont(new Font("微软雅黑", Font.PLAIN, 16));
            gbc.gridx = 0;
            gbc.gridy = i;
            inputPanel.add(inputLabels[i], gbc);

            inputFields[i] = new JTextField(15);
            inputFields[i].setFont(new Font("微软雅黑", Font.PLAIN, 16));
            gbc.gridx = 1;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            inputPanel.add(inputFields[i], gbc);

            // 添加输入提示标签
            JLabel promptLabel = new JLabel("输入你知道客户的相关信息查询");
            promptLabel.setFont(new Font("微软雅黑", Font.PLAIN, 12));
            promptLabel.setForeground(Color.GRAY);
            gbc.gridx = 2;
            gbc.insets = new Insets(5, 10, 5, 10); // 重新设置间距
            inputPanel.add(promptLabel, gbc);
        }

        add(inputPanel, BorderLayout.NORTH);

        // 创建表格模型
        String[] columnNames = {"客户编号", "公司名称", "公司简称", "公司地址", "公司电话", "公司邮件", "联系人姓名", "联系人电话"};
        tableModel = new DefaultTableModel(columnNames, 0);
        resultTable = new JTable(tableModel);
        resultTable.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        resultTable.setRowHeight(25);

        // 添加表格到滚动面板
        JScrollPane scrollPane = new JScrollPane(resultTable);
        add(scrollPane, BorderLayout.CENTER);

        // 创建按钮面板
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 10));
        JButton searchButton = new JButton("搜索");
        searchButton.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        searchButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                performSearch();
            }
        });
        buttonPanel.add(searchButton);

        JButton returnButton = new JButton("返回");
        returnButton.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        returnButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                GuanliPanel.setContent(new CustomerPanel());
            }
        });
        buttonPanel.add(returnButton);

        add(buttonPanel, BorderLayout.SOUTH);

        // 设置窗口大小并显示
        pack();
        setVisible(true);
    }

    // 执行搜索的方法
    private void performSearch() {
        Customer customerQuery = new Customer();
        customerQuery.setCid(inputFields[0].getText());
        customerQuery.setCcompanyName(inputFields[1].getText());
        customerQuery.setCcompanySName(inputFields[2].getText());
        customerQuery.setCcompanyAddress(inputFields[3].getText());
        customerQuery.setCcompanyPhone(inputFields[4].getText());
        customerQuery.setCemail(inputFields[5].getText());
        customerQuery.setCName(inputFields[6].getText());
        customerQuery.setCtelPhone(inputFields[7].getText());

        List<Customer> customers = customerMapper.selectCustomer(customerQuery);

        if (customers.isEmpty()) {
            JOptionPane.showMessageDialog(this, "没有符合条件的客户信息！", "提示", JOptionPane.INFORMATION_MESSAGE);
        }

        updateTableData(customers);
    }

    // 更新表格数据的方法
    private void updateTableData(List<Customer> customers) {
        // 清空表格
        tableModel.setRowCount(0);

        // 添加新的数据
        for (Customer customer : customers) {
            Object[] row = {
                    customer.getCid(), customer.getCcompanyName(), customer.getCcompanySName(), customer.getCcompanyAddress(),
                    customer.getCcompanyPhone(), customer.getCemail(), customer.getCName(),
                    customer.getCtelPhone()
            };
            tableModel.addRow(row);
        }
    }
}
